#!/usr/bin/python

import os, sys
import gtk
sys.path.append(os.getcwd() + "/widget")
sys.path.append(os.getcwd() + "/mod")
import initialize
import remote_tree
import local_tree
import systray
try:
    import psyco
    psyco.full()
except ImportError:
    print 'Absynth supports "psyco", an inline optimizer for python' \
          'code, you can get it at http://sourceforge.net/projects/psyco/'

class Absynth:
    def __init__(self):
        gtk.window_set_auto_startup_notification(True)
        self.initialize_absynth()
        self.log.new(self, 'Grazie per partecipare allo sviluppo di Absynth FTP :-) \n')
        
    def initialize_absynth(self):
        initialize.mods(self)
        initialize.locations(self)
        initialize.log_view(self)
        initialize.systray(self)
        initialize.treeview_remote(self)
        initialize.localpath(self)
        initialize.treeview_local(self)
        initialize.treeview_dw(self)
        initialize.local_popup(self)
        initialize.remote_popup(self)
    
    ##########
    #Callbacks
    ##########
    def about_dialog(self, *args):
        about =  gtk.glade.XML("absynth-ftp.glade", "about")
    
    def on_connect(self, *args):
        if self.connection.get_status() == False:
            try:
                self.connection.connect_to ( self.host.get_text(), self.user.get_text(), self.password.get_text() )
            except:
                self.log.new (self, 'Impossibile connettersi \n')
                return 1
            
            try:
                self.notify.new ( self.connection.welcome() )
            except:
                self.log.new (self, 'Messaggio di welcome non ricevuto \n')

            self.AbMainWindow.set_title( "Absynth FTP <<%s>>" % self.host.get_text() )
            remote_tree.populate(self)
            self.update_remotepath()
            self.connectbutton.set_label('Disconnetti')    
            
        else:
            try:
                self.connection.close()
            except: 
                self.log.new(self, 'Errore nella chiusura della connessione \n')
                return 1
            self.connectbutton.set_label("Connetti")
            self.AbMainWindow.set_title("Absynth FTP")
            self.remote_treemodel.clear()
            self.remote_path.set_text('')
            
    def connect_enter(self, widget, event):
        if 'Return' == gtk.gdk.keyval_name(event.keyval):
            self.on_connect()
            
    #Remote treeview callbacks       
    def get_selected_remote_name(self):
        treeselection = self.remote_treeview.get_selection()
        (tmp_model, tmp_iter) = treeselection.get_selected()
        name = tmp_model.get_value(tmp_iter, 2)
        return name
        
    def remote_treeview_clicked(self, *args):
        remote_tree.treeview_clicked(self)
        
    def remote_treeview_button_press_event(self, treeview, event):
        remote_tree.on_treeview_button_press_event(self, treeview, event)
    
    def remote_delete(self, *args):
        treeselection = self.remote_treeview.get_selection()
        (tmp_model, tmp_iter) = treeselection.get_selected()
        name = tmp_model.get_value(tmp_iter, 3)
        type = tmp_model.get_value(tmp_iter, 0)
        if type == "file":
            self.connection.delete(name)
        else:
            self.connection.rmdir(name)
        local_tree.populate(self)
    
    def remote_update(self, *args):
        remote_tree.populate(self)
        
    def update_remotepath(self):
        self.remote_path.set_text( self.connection.pwd() )
    
    def remote_up(self, *args):
        self.connection.cwd('..')
        remote_tree.populate(self)
        self.update_remotepath()
        
    #Local treeview callbacks  
    def get_selected_local_name(self):
        treeselection = self.local_treeview.get_selection()
        (tmp_model, tmp_iter) = treeselection.get_selected()
        name = tmp_model.get_value(tmp_iter, 2)
        return name
    
    def local_treeview_clicked(self, *args):
        local_tree.treeview_clicked(self)
        
    def local_treeview_button_press_event(self, treeview, event):
        local_tree.on_treeview_button_press_event(self, treeview, event)
    
    def upload_file(self, *args):
        from ftpmod import Upload
        file = self.get_selected_local_name()
        up = Upload(file, self.remote_path, self.host, self.user, self.password)
        up.start()
        
    def update_localpath(self):
        self.local_path.set_text( self.local.pwd() )
      
    def local_up(self, *args):
        self.local.cwd('..')
        local_tree.populate(self)
        self.update_localpath()
    
    def local_reload(self, *args):
        local_tree.populate(self)
        
    def local_delete(self, *args):
        treeselection = self.local_treeview.get_selection()
        (tmp_model, tmp_iter) = treeselection.get_selected()
        name = tmp_model.get_value(tmp_iter, 2)
        type = tmp_model.get_value(tmp_iter, 0)
        if type == "directory":
            self.local.rmdir(name)
        else:
            self.local.delete(name)
        local_tree.populate(self)
        
    #Systray callback
    def tray_show_menu(self, status_icon, button, activate_time):
        systray.trayMenu(self, status_icon, button, activate_time)
        
       
        
    def main(self):
        gtk.gdk.threads_init()
        gtk.gdk.threads_enter()
        gtk.main()
        gtk.gdk.threads_leave()
        
if __name__ == "__main__":
    Absynth = Absynth()
    Absynth.main()
